/*******************************************************************************
* @file usart.h
* @author hai
* @version V1.0
* @date 2017-8-17
* @brief
********************************************************************************/

#ifndef __UART_H_
#define __UART_H_

#include "stm32f10x.h"
#include "system.h"
#include "Dtstruct.h"
#include "uorb.h"

enum {  
    USART1_CH,
    USART2_CH,
    USART3_CH,
    UART_NUM
};

typedef struct {
    INT32U status;
    INT32U baud;
    FIFO sfifo;
    FIFO rfifo;
    INT8U *txbuf;
    INT8U *rxbuf;
}UART_STRUCT;

typedef struct
{
	uint16_t len;
	uint8_t buf[128];
}usart_recv_publish_t;

ORB_DECLARE(usart1_update_recv);
ORB_DECLARE(usart2_update_recv);
ORB_DECLARE(usart3_update_recv);

extern UART_STRUCT uarts[UART_NUM];

BOOLEAN uart_busy(INT8U chan);
BOOLEAN uart_opened(INT8U chan);
uint32_t uart_reads(uint8_t chx, uint8_t *rbuf,uint16_t size);
BOOLEAN uart_write(INT8U chan, INT8U unit);
BOOLEAN uart_writemass(INT8U chan, INT8U *mass, INT16U len);
BOOLEAN uart_received(INT8U chan);
BOOLEAN uart_read(INT8U chan, INT8U *ch);
void uart_close(INT8U chan);

void usart_init(void);
BOOLEAN uart_setup(INT8U chan,INT32U baud,INT32U txsize,INT32U rxsize);
void usart_publish_loop(void);

#endif

